package Lee_code;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author blh
 * @date 2024年07月29日 16:35
 */
public class Lee_678 {
    public boolean checkValidString(String s) {
        int len = s.length();
        Deque<Integer> left = new ArrayDeque<>();
        Deque<Integer> star = new ArrayDeque<>();
        for (int i = 0; i < len; i++) {
            char temp = s.charAt(i);
            if (temp == '(') {
                left.push(i);
            } else if (temp == '*') {
                star.push(i);
            } else if (temp == ')') {
                if (!left.isEmpty()) {
                    left.pop();
                } else if (!star.isEmpty()) {
                    star.pop();
                } else {
                    return false;
                }
            }
        }
        while (!left.isEmpty() && !star.isEmpty()){
            int starTop = star.peek();
            int leftTop = left.peek();
            if (starTop>leftTop){
                star.pop();
                left.pop();
            } else {
                return false;
            }
        }
        return left.isEmpty();
    }
}
